package cn.sharp.bbs.service.impl;

import cn.sharp.bbs.common.Utils;
import cn.sharp.bbs.common.page.PageParam;
import cn.sharp.bbs.dao.gen.BillMapper;
import cn.sharp.bbs.dao.gen.DatadictMapper;
import cn.sharp.bbs.dao.score.BillDao;
import cn.sharp.bbs.entity.BO.BillIn;
import cn.sharp.bbs.entity.BO.BillOut;
import cn.sharp.bbs.entity.DO.Bill;
import cn.sharp.bbs.entity.DO.Datadict;
import cn.sharp.bbs.service.BillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @className: BillServiceImpl
 * @description: 积分账单服务实现类
 * @author: 王晓崧
 * @date: 2022/7/9
 **/
@Service
@Transactional
public class BillServiceImpl implements BillService {

    @Resource
    private BillDao billDao;

    @Resource
    private BillMapper billMapper;

    private DatadictMapper datadictMapper;

    /**
     * 获取当前用户的积分收入账单
     *
     * @param u_id      用户id
     */
    @Override
    public Map<String, Object> getInBill(BillIn billIn) {
        return Utils.getPage(billIn, () -> billDao.getAllInBill(billIn));
    }

    private List<BillIn> getAllInBill(Long u_id) {
        List<Bill> billList = billDao.getInBill(u_id);//从数据库中拿到当前用户的原始收入账单
        List<BillIn> billIns = new ArrayList<>();//创建收入账单列表
        for (Bill bill : billList) {
            BillIn billIn = new BillIn();
            //获取账单项对应的类型并设置收入类型
            Datadict datadict = new Datadict();
            datadict.setType("bill_type");
            datadict.setCode(bill.getType());
            billIn.setType(billDao.getNameByDict(datadict));
            billIn.setNum(bill.getNum());//设置收入数值
            //根据收入类型将来源id进行再加工
            if (billIn.getType().equals("任务")) {
                billIn.setMark("r" + bill.getFrom());
            } else {
                billIn.setMark("t" + bill.getFrom());
            }
            billIn.setTime(bill.getTime());//设置收入时间
            billIns.add(billIn);//将收入项填充到收入列表中
        }
        return billIns;
    }

    /**
     * 获取当前用户的支出账单
     *
     * @param u_id 用户id
     */
    @Override
    public Map<String, Object> getOutBill( BillOut billOut) {
        return Utils.getPage(billOut, () -> billDao.getAllOuTBill(billOut));
    }

    private List<BillOut> getAllOuTBill(Long u_id) {
        List<Bill> billList = billDao.getOutBill(u_id);//从数据库中拿到当前用户的原始支出账单
        List<BillOut> billOuts = new ArrayList<>();//创建支出账单列表
        for (Bill bill : billList) {
            BillOut billOut = new BillOut();
            //获取账单项对应的类型并设置支出类型
            Datadict datadict = new Datadict();
            datadict.setType("bill_type");
            datadict.setCode(bill.getType());
            billOut.setType(billDao.getNameByDict(datadict));
            billOut.setNum(bill.getNum());//设置支出数值
            billOut.setMark("t" + bill.getFrom());//再加工来源id
            billOut.setTime(bill.getTime());//设置支出时间
            billOuts.add(billOut);//将支出项填充到支出列表中
        }
        return billOuts;
    }
}
